package com.imooc.controller;

import com.imooc.bo.PeopleBO;
import com.imooc.service.UserInterface;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "用于注册登录的相关接口",tags = "用于注册登录的相关接口")
@RestController
@RequestMapping("passport")
public class PassportController {
    @Autowired
    private UserInterface userInterface;

    /**
     * @RequestParam 请求类型的参数而不是路径参数
     * @param username
     * @return
     */
    @GetMapping("/userNameIsExist")
    @ApiOperation(value = "用户名是否存在",notes = "用户名是否存在",httpMethod = "GET",produces="application/json")
    @ApiImplicitParam(value = "用户名",name = "username",required = true,defaultValue = "zhangsan",paramType = "query")
    public IMOOCJSONResult userNameIsExist(String username){
        // 1、判断用户名不能为空
        if (StringUtils.isBlank(username)){
            return IMOOCJSONResult.errorMsg("用户名不能为空");
        }
        // 2 查找注册的用户名是否存在
        boolean isExit = userInterface.queryUserNameIsExit(username);

        if (isExit){
            return IMOOCJSONResult.errorMsg("存在重复的用户名");
        }

        // 3请求成功 用户名没有存在
        return IMOOCJSONResult.ok();
    }
    @ApiOperation(value = "注册用户",notes = "注册用户",httpMethod = "POST")
    @RequestMapping("/register")
    public IMOOCJSONResult regist(@RequestBody PeopleBO peopleBO){
        String username = peopleBO.getUsername();
        String password = peopleBO.getPassword();
        String confirmPwd = peopleBO.getConfirmPassword();

        // 0.判断用户名和密码必须不为空
        if(StringUtils.isBlank(username) ||
                StringUtils.isBlank(password)||
                StringUtils.isBlank(confirmPwd)
        ){
            IMOOCJSONResult.errorMsg("用户名和密码不能为空");
        }
        // 1、 查询用户名是否存在
        Boolean isExit = userInterface.queryUserNameIsExit(username);
        if (isExit){
            return IMOOCJSONResult.errorMsg("用户名已经存在");
        }
        // 2 密码长度不能少于16位
        if(password.length() < 6){
            return IMOOCJSONResult.errorMsg("密码长度不能小于6");
        }
        // 3 判断令慈密码是否一致
        if(!password.equals(confirmPwd)){
            return IMOOCJSONResult.errorMsg("两次密码输入不一致");
        }
        // 4 实现注册
        userInterface.createPeople(peopleBO);
        return IMOOCJSONResult.ok();
    }
}
